package com.tpc.client.systeme.core.datasource;

import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tpc.dao.extend.Contact;
import com.tpc.client.modules.core.contact.service.ContactService;
import com.tpc.client.modules.core.contact.service.ContactServiceAsync;

public class ContactDataSource extends GwtRpcDataSource {

    String strurl;

    public ContactDataSource(String strID) {
            setID(strID);
            DataSourceIntegerField dsfId_miniorgctc = new DataSourceIntegerField("id_minictcorg");
            dsfId_miniorgctc.setPrimaryKey(Boolean.TRUE);
            DataSourceTextField dsfName_miniorgctct = new DataSourceTextField("name_minictcorg", "Name");
            DataSourceTextField dsfUsername_miniorgctct = new DataSourceTextField("miniorgctc_username", "Nom d'usager");
            DataSourceTextField dsfPassword_miniorgctc = new DataSourceTextField("miniorgctc_password", "Mot de passe");
            DataSourceTextField dsfEmail_miniorgctc = new DataSourceTextField("email_minictcorg", "Email");
            DataSourceTextField dsfPhone_miniorgctc = new DataSourceTextField("phonebusiness_minictcorg", "Phone");
            DataSourceTextField dsfNot_miniorgctc = new DataSourceTextField("note_minictcorg", "Note");

            

            setFields(dsfId_miniorgctc,dsfName_miniorgctct,dsfUsername_miniorgctct,dsfPassword_miniorgctc,dsfEmail_miniorgctc,dsfPhone_miniorgctc,dsfNot_miniorgctc);

            strurl = Page.getAppDir();
            strurl = strurl.substring(0,strurl.length()-1);
            strurl = strurl.substring(strurl.lastIndexOf("/"));
    }

    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
        Criteria criteria = request.getCriteria();
        String organisationTerm = null;
        if (criteria != null) {
            if (criteria.getAttributeAsString("organisationId") != null) {
                organisationTerm = criteria.getAttributeAsString("organisationId");
            }
        }
GWT.log("organisationId:"+organisationTerm);
        ContactServiceAsync service = GWT.create(ContactService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(strurl+"/com.tpc.contact/contactloader");
        service.loadListContact(organisationTerm,new AsyncCallback<Contact[]> () {
            public void onFailure (Throwable caught) {
                response.setStatus (RPCResponse.STATUS_FAILURE);
                processResponse (requestId, response);
            }
            public void onSuccess (Contact[] result) {
                ListGridRecord[] list = null;
                if (result != null) {
                    list = new ListGridRecord[result.length];
                    for (int i = 0; i < result.length; i++) {
                        list[i] = new ListGridRecord();
                        list[i].setAttribute("id_minictcorg",result[i].getId());
                        list[i].setAttribute("miniorgctc_username",result[i].getUsername());
                        list[i].setAttribute("miniorgctc_password",result[i].getPassword());
                        list[i].setAttribute("name_minictcorg", result[i].getFirstName()+" "+result[i].getLastName());
                        if (result[i].getPhoneBusiness() != null) {
                            list[i].setAttribute("phonebusiness_minictcorg", result[i].getPhoneBusiness().getAreacode()+" "+result[i].getPhoneBusiness().getNumber());
                        }
                        if (result[i].getEmail() != null) {
                            list[i].setAttribute("email_minictcorg",result[i].getEmail().getUrl());
                        }
                        list[i].setAttribute("note_minictcorg", result[i].getDescription());
                    }
                }
                else {
                    list[0] = new ListGridRecord();
                    list[0].setAttribute("id_minictcorg","");
                    list[0].setAttribute("name_minictcorg","");
                    list[0].setAttribute("miniorgctc_username","");
                    list[0].setAttribute("miniorgctc_password","");
                    list[0].setAttribute("phonebusiness_minictcorg","");
                    list[0].setAttribute("note_minictcorg","");
                }
                response.setData(list);
                processResponse(requestId, response);
            }
        });

    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        //@TODO : fdsa
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }
}
